let _ = require('lodash');

class AdvancedImageLoader {
  constructor() {
    this.images = [];
    this.loading = 0;
    this.total = 0;
  }

  add(url) {
    return new Promise((resolve, reject) => {
      const img = new Image();
      img.src = url;
      img.onload = () => {
        this.images.push(img);
        resolve(img);
        this.updateProgress();
      };
      img.onerror = reject;
      this.loading++;
      this.total++;
    });
  }

  updateProgress() {
    const progress = (this.images.length / this.total) * 100;
    console.log(`加载进度: ${progress.toFixed(1)}%`);
    // 可以更新页面上的进度条元素
  }

  loadAll(...urls) {
    return Promise.all(urls.map((url) => this.add(url))).then(() => {
      console.log('所有图片加载完成');
      return this.images;
    });
  }
}

export { AdvancedImageLoader };
